package cn.shop.merchant.service;

import cn.hutool.core.util.StrUtil;
import cn.shop.transcation.detail.entity.FundDetail;
import cn.shop.transcation.detail.entity.TransactionDetail;
import cn.shop.transcation.detail.entity.TransactionRecordQueryReq;
import cn.shop.transcation.detail.mapper.FundDetailMapper;
import cn.shop.utils.PageUtil;
import cn.shop.utils.Putif;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author : 李一涵
 * @date : Created in 2021-11-10 9:48
 * @description : 明细service
 */
@Service
public class FundDetailService extends BaseServiceImpl<FundDetailMapper, FundDetail>{

    /**
     * 获取资金明细列表
     *
     * @param uid 商户编号
     * @param req 查询条件
     */
    public Map<String, Object> details(long uid, TransactionRecordQueryReq req) {
        Page<TransactionDetail> page = Page.of(req.getCurrent(), req.getSize());
        req.setUid(uid);
        page = baseMapper.detailList(page, null, req);
        return PageUtil.formatPage(page, this::format);
    }

    /**
     * 统计信息
     *
     * @param uid 商户类型
     * @param req 查询条件
     * @return 返回结果
     */
    public Map<String, Object> statistics(long uid, TransactionRecordQueryReq req) {
        req.setUid(uid);
        return baseMapper.sumAmountQuery(req);
    }

    /**
     * 格式化明细
     *
     * @param detail 明细信息
     */
    private Map<String, Object> format(TransactionDetail detail) {
        List<String> types = StrUtil.split(detail.getPaySolution(), "#");
        return Putif.start()
                .anyway("id", detail.getId())
                .anyway("uid", detail.getUid())
                .anyway("orderId", detail.getOrderId())
                .anyway("goods", detail.getGoods())
                .anyway("amount", detail.getAmount())
                .anyway("type", detail.getType())
                .anyway("partitionName", detail.getPartitionName())
                .anyway("gameName", detail.getGameName())
                .anyway("playAccount", detail.getPlayerAccount())
                .anyway("share", detail.getMerchantShare())
                .anyway("sellerShare", detail.getSellerShare())
                .anyway("createTime", detail.getCreateTime())
                .anyway("paySolution", types.isEmpty() ? "" : types.get(0))
                .get();
    }

}
